﻿using System;
using System.Configuration;
using System.Threading;
using System.ComponentModel;

public partial class AlbumCreator : AlbumPage
{
    //protected string _albumId;
    protected bool _nameIsValid;

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void wizardAlbumCreate_Load(object sender, EventArgs e)
    {
        try
        {
            wizardAlbumCreate.ActiveStepIndex = int.Parse(Server.HtmlEncode(Server.UrlDecode(Request.QueryString[AlbumHelper.StepForWizard])));
        }
        catch { }
    }

    protected void CreateAlbum()
    {
            DateTime dateToday = DateTime.UtcNow;
            UserDataHandler user = new UserDataHandler();

            if (txtBoxAlbumName.Text != "")
            {
                _nameIsValid = true;
                string path = "~\\" + ConfigurationManager.AppSettings["folderForImages"];
                Album album = new Album(0, txtBoxAlbumName.Text, user.GetCurrentUserName(), path, dateToday);
                album.ID = AlbumManager.CreateAlbum(album);
                album.FolderPath = path + "\\" + album.ID.ToString();
                //_albumId = album.ID.ToString();
                albumId.Value = album.ID.ToString();

                AlbumManager.UpdateAlbum(album);

                PhotoManager.CreateAlbumFolder(Server.MapPath(ConfigurationManager.AppSettings["folderForImages"]), album.ID);
                string authority = Request.Url.Authority.ToString();
                emailControl.MessageText = "http://" + authority + "/confirmit/AlbumViewer.aspx?" + AlbumHelper.AlbumIdKeyString + "=" + Server.UrlEncode(album.ID.ToString());
            }
            else _nameIsValid=false;
    }

    protected void SendMail()
    {
        string resourceKey = EmailManager.SendEmail(emailControl.Adressees, emailControl.MessageText);
        emailControl.Errors = (string)GetLocalResourceObject(resourceKey);
    }
    protected void wizardAlbumCreate_NextButtonClick(object sender, System.Web.UI.WebControls.WizardNavigationEventArgs e)
    {
        if (wizardAlbumCreate.ActiveStepIndex == 0)
        {
            CreateAlbum();
            if (_nameIsValid)
            {
                wizardAlbumCreate.ActiveStepIndex = 1;
            }
            else
            {
                Response.Redirect("~/AlbumCreator.aspx");
            }
        }
        else
        if (wizardAlbumCreate.ActiveStepIndex == 1)
        {
            if (chkboxSendMail.Checked)
            {
                wizardAlbumCreate.ActiveStepIndex = 2;
            }
            else
            {
                wizardAlbumCreate.ActiveStepIndex = 3;
            }
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        SendMail();
    }
    protected void FinishButton_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~/AlbumViewer.aspx?" + AlbumHelper.AlbumIdKeyString + "=" + Server.UrlEncode(albumId.Value));
    }
}
